//
//  MyDepositViewModel.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/21.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "MyDepositViewModel.h"
#import "UserManager.h"

@implementation MyDepositViewModel

- (instancetype)init
{
    if (self = [super init]) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.listSuccessObject     = [RACSubject subject];
    self.listFailureObject     = [RACSubject subject];

    self.takeOriginalSuccessObject = [RACSubject subject];
    self.takeOriginalFailureObject = [RACSubject subject];
    
    self.investStateSuccess = [RACSubject subject];
    self.investStateFailure = [RACSubject subject];
    
    self.continueSuccessObject = [RACSubject subject];
    self.continueFailureObject = [RACSubject subject];

}

-(void)exchangeDataWithPage:(NSString *)page
{
    
    @weakify(self);
    
    [UserManager investListWithUserId:[CBTLocalUserManager sharedInstance].userId page:page block:^(id data, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal) {
            [self_weak_.listSuccessObject sendNext:[MyInvestListModel mj_objectWithKeyValues:data[@"data"]]];
        }else{
            [self_weak_.listFailureObject sendNext:nil];
        }
    }];
    
}


-(void)takedrawCapitalOrIncomeWithInvestId:(NSString *)investId
{
    @weakify(self);
   
    [UserManager drawCapitalOrIncomeWithUserId:[CBTLocalUserManager sharedInstance].userId investId:investId block:^(id data, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal) {
            [HFStatusView showSuccessMessage:kLocalizedString(@"提取成功",nil)];
            [self_weak_.takeOriginalSuccessObject sendNext:nil];
        }else{
            [HFStatusView showFailureMessage:msg];
            [self_weak_.takeOriginalFailureObject sendNext:nil];
        }
    }];
}

-(void)takedrawCapitalOrIncomeWithUserId:(NSString *)userID investId:(NSString *)investId indexPath:(NSInteger)indexPath
{
    @weakify(self);
    
    [UserManager getInvestStateWithUserId:[CBTLocalUserManager sharedInstance].userId investId:investId block:^(id data, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal) {
            [self_weak_.investStateSuccess sendNext:@{@"code":[data[@"data"]objectForKey:@"expire"],@"msg":[data[@"data"]objectForKey:@"result"],@"indexPath":NSStringInt(indexPath)}];
        }else{
            [HFStatusView showFailureMessage:msg];
            [self_weak_.investStateFailure sendNext:nil];
        }
    }];
}

-(void)userContinueInvestWithInvestId:(NSString *)investId {
    
    @weakify(self);
    
    [UserManager getUserContinueInvestWithInvestId:investId block:^(id data, HttpRequestResult result, NSString *msg) {
        if (result == kHttpRequestResultNormal) {
            [HFStatusView showSuccessMessage:kLocalizedString(@"续投成功",nil)];
            [self_weak_.continueSuccessObject sendNext:@""];
        }else{
            [HFStatusView showFailureMessage:msg];
            [self_weak_.continueFailureObject sendNext:nil];
        }
    }];
}

@end
